<template>
  <el-dialog
    :title="!dataForm.messageId ? '新增通知' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-col :span="12">
        <el-form-item label="消息标题" prop="messageTile">
          <el-input v-model="dataForm.messageTile" :disabled="disabled" placeholder="消息标题"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="消息内容" prop="messageContent">
          <el-input v-model="dataForm.messageContent" :disabled="disabled" placeholder="消息内容"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="消息类型" prop="messageType">
          <el-input v-model="dataForm.messageType" :disabled="disabled" placeholder="消息类型"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="发送时间" prop="sendTime">
          <el-input v-model="dataForm.sendTime" :disabled="disabled" placeholder="发送时间"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="发送人" prop="senter">
          <el-input v-model="dataForm.senter" :disabled="disabled" placeholder="发送人"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="messageStatus">
          <el-input v-model="dataForm.messageStatus" :disabled="disabled" placeholder="消息状态"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="读取状态" prop="readStatus">
          <el-input v-model="dataForm.readStatus" :disabled="disabled" placeholder="读取状态"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="接收人" prop="geter">
          <el-input v-model="dataForm.geter" :disabled="disabled" placeholder="消息接收人"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="接收人类型" prop="geterType">
          <el-input v-model="dataForm.geterType" :disabled="disabled" placeholder="消息接收人类型"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          messageId: '',
          messageTile: '',
          messageContent: '',
          messageType: '',
          sendTime: '',
          senter: '',
          messageStatus: '',
          readStatus: '',
          geter: '',
          geterType: ''},
        dataRule: {
          messageTile: [
            {required: true, message: '标题不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (messageId, disabled) {
        this.disabled = disabled
        this.dataForm.messageId = messageId || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.messageId) {
            this.$http({
              url: `/sys/message/info/${this.dataForm.messageId}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.message
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/sys/message/${!this.dataForm.messageId ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
